package com.project.miaosha.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.project.miaosha.domain.User;
import com.project.miaosha.redis.RedisService;
import com.project.miaosha.redis.UserKey;
import com.project.miaosha.result.CodeMsg;
import com.project.miaosha.result.Result;
import com.project.miaosha.service.UserService;  
  
@Controller  
@RequestMapping("/demo")
public class DemoController {
    @Autowired
    private UserService userService;
    @Autowired
    RedisService redisService;

 	@RequestMapping("/")  
    @ResponseBody  
    String home() {  
        return "Hello World!";  
    }  
 	@RequestMapping("/hello")  
    @ResponseBody  
    public Result<String> hello() {  
        return Result.success("Hello World!");  
    }  
 	
 	@RequestMapping("/helloError")  
    @ResponseBody  
    public Result<String> helloError() {  
        return Result.error(CodeMsg.SERVER_ERROR);  
    }
 	@RequestMapping("/thymeleaf")  
    public String thymeleaf(Model model) {  
 		model.addAttribute("name","zhang");
 		System.out.println("hello");
        return "hello";  
    }
 	@RequestMapping("/db/get")  
    @ResponseBody  
    public Result<User> dbGet() {  
 		User user = userService.getById(1);
        return Result.success(user);  
    }
 	@RequestMapping("/db/tx")  
    @ResponseBody  
    public Result<Boolean> dbTx() {  
 		userService.tx();
        return Result.success(true);  
    }
 	@RequestMapping("/redis/get")  
    @ResponseBody  
    public Result<User> get() {  
 		User user = redisService.get(UserKey.getById,""+1,User.class);
        return Result.success(user);  
    }
 	@RequestMapping("/redis/set")  
    @ResponseBody  
    public Result<Boolean> set() {  
 		User user = new User();
 		user.setId(1);
 		user.setName("zhang");
 		redisService.set(UserKey.getById,""+1,user);
        return Result.success(true);  
    }
}
